﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CFZS.IDAL;
using CFZS.FrameWork.Utility;

namespace CFZS.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:BaseRole
    /// </summary>
    public partial class BaseRole : IBaseRole
    {
        public BaseRole()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from BaseRole");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.BigInt)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(CFZS.Model.BaseRole model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into BaseRole(");
            strSql.Append("Name,SymCode,Img,Descr,IsActive,LinenNo,CreatedAt,CreatedBy,UpdatedAt,UpdatedBy)");
            strSql.Append(" values (");
            strSql.Append("@Name,@SymCode,@Img,@Descr,@IsActive,@LinenNo,@CreatedAt,@CreatedBy,@UpdatedAt,@UpdatedBy)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@SymCode", SqlDbType.VarChar,50),
					new SqlParameter("@Img", SqlDbType.VarChar,200),
					new SqlParameter("@Descr", SqlDbType.Text),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@LinenNo", SqlDbType.BigInt,8),
					new SqlParameter("@CreatedAt", SqlDbType.DateTime),
					new SqlParameter("@CreatedBy", SqlDbType.VarChar,50),
					new SqlParameter("@UpdatedAt", SqlDbType.DateTime),
					new SqlParameter("@UpdatedBy", SqlDbType.VarChar,50)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.SymCode;
            parameters[2].Value = model.Img;
            parameters[3].Value = model.Descr;
            parameters[4].Value = model.IsActive;
            parameters[5].Value = model.LinenNo;
            parameters[6].Value = model.CreatedAt;
            parameters[7].Value = model.CreatedBy;
            parameters[8].Value = model.UpdatedAt;
            parameters[9].Value = model.UpdatedBy;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CFZS.Model.BaseRole model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update BaseRole set ");
            strSql.Append("Name=@Name,");
            strSql.Append("SymCode=@SymCode,");
            strSql.Append("Img=@Img,");
            strSql.Append("Descr=@Descr,");
            strSql.Append("IsActive=@IsActive,");
            strSql.Append("LinenNo=@LinenNo,");
            strSql.Append("CreatedAt=@CreatedAt,");
            strSql.Append("CreatedBy=@CreatedBy,");
            strSql.Append("UpdatedAt=@UpdatedAt,");
            strSql.Append("UpdatedBy=@UpdatedBy");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@SymCode", SqlDbType.VarChar,50),
					new SqlParameter("@Img", SqlDbType.VarChar,200),
					new SqlParameter("@Descr", SqlDbType.Text),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@LinenNo", SqlDbType.BigInt,8),
					new SqlParameter("@CreatedAt", SqlDbType.DateTime),
					new SqlParameter("@CreatedBy", SqlDbType.VarChar,50),
					new SqlParameter("@UpdatedAt", SqlDbType.DateTime),
					new SqlParameter("@UpdatedBy", SqlDbType.VarChar,50),
					new SqlParameter("@Id", SqlDbType.BigInt,8)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.SymCode;
            parameters[2].Value = model.Img;
            parameters[3].Value = model.Descr;
            parameters[4].Value = model.IsActive;
            parameters[5].Value = model.LinenNo;
            parameters[6].Value = model.CreatedAt;
            parameters[7].Value = model.CreatedBy;
            parameters[8].Value = model.UpdatedAt;
            parameters[9].Value = model.UpdatedBy;
            parameters[10].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from BaseRole ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.BigInt)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from BaseRole ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CFZS.Model.BaseRole GetModel(long Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Name,SymCode,Img,Descr,IsActive,LinenNo,CreatedAt,CreatedBy,UpdatedAt,UpdatedBy from BaseRole ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.BigInt)
			};
            parameters[0].Value = Id;

            CFZS.Model.BaseRole model = new CFZS.Model.BaseRole();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = long.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SymCode"] != null && ds.Tables[0].Rows[0]["SymCode"].ToString() != "")
                {
                    model.SymCode = ds.Tables[0].Rows[0]["SymCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Img"] != null && ds.Tables[0].Rows[0]["Img"].ToString() != "")
                {
                    model.Img = ds.Tables[0].Rows[0]["Img"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Descr"] != null && ds.Tables[0].Rows[0]["Descr"].ToString() != "")
                {
                    model.Descr = ds.Tables[0].Rows[0]["Descr"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsActive"] != null && ds.Tables[0].Rows[0]["IsActive"].ToString() != "")
                {
                    model.IsActive = int.Parse(ds.Tables[0].Rows[0]["IsActive"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LinenNo"] != null && ds.Tables[0].Rows[0]["LinenNo"].ToString() != "")
                {
                    model.LinenNo = long.Parse(ds.Tables[0].Rows[0]["LinenNo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreatedAt"] != null && ds.Tables[0].Rows[0]["CreatedAt"].ToString() != "")
                {
                    model.CreatedAt = DateTime.Parse(ds.Tables[0].Rows[0]["CreatedAt"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreatedBy"] != null && ds.Tables[0].Rows[0]["CreatedBy"].ToString() != "")
                {
                    model.CreatedBy = ds.Tables[0].Rows[0]["CreatedBy"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UpdatedAt"] != null && ds.Tables[0].Rows[0]["UpdatedAt"].ToString() != "")
                {
                    model.UpdatedAt = DateTime.Parse(ds.Tables[0].Rows[0]["UpdatedAt"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UpdatedBy"] != null && ds.Tables[0].Rows[0]["UpdatedBy"].ToString() != "")
                {
                    model.UpdatedBy = ds.Tables[0].Rows[0]["UpdatedBy"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name,SymCode,Img,Descr,IsActive,LinenNo,CreatedAt,CreatedBy,UpdatedAt,UpdatedBy ");
            strSql.Append(" FROM BaseRole ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,Name,SymCode,Img,Descr,IsActive,LinenNo,CreatedAt,CreatedBy,UpdatedAt,UpdatedBy ");
            strSql.Append(" FROM BaseRole ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM BaseRole ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from BaseRole T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "BaseRole";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
